﻿// ***********************************************************************
// Assembly         : TxOsgViewer
// Author           : tianteng
// Created          : 07-01-2019
//
// Last Modified By : tianteng
// Last Modified On : 05-09-2020
// ***********************************************************************
// <copyright file="Program.cs" company="北京腾雪科技有限责任公司">
//     Copyright © 北京腾雪科技有限责任公司 2022
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Tx;
using Tx.OSG.UI;
using Tx.OSG;
using Tx.Addins;
using Tx.Sys;
using TxOsgViewer.Properties;
using MessageBoxButtons = System.Windows.Forms.MessageBoxButtons;
using Tx.UI;
using TxOsgViewer.TestCmds;

namespace TxOsgViewer
{
    /// <summary>
    /// Class Program.
    /// </summary>
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);


            var commandArgs = new CmdLineArgs(args);

            var osgViewForm = new SimpleOsgViewForm();
            osgViewForm.Icon = Resources.腾雪科技;
            osgViewForm.Text = "OSG查看器";
            InitMenu(osgViewForm.menuStrip, osgViewForm.App);

            ReadInFile(osgViewForm.ViewCtrl.OsgObj, commandArgs);

            Application.Run(osgViewForm);
        }

        private static void InitMenu(MenuStrip menuStrip, AppService appService)
        {
            menuStrip.Items.AddFileMenu(appService);
            menuStrip.Items.AddViewMenu(appService);

            menuStrip.Items.Item<ToolStripMenuItem>("测试(&T)").DropDownItems
                .AddItem<ToolStripMenuItem, CreactRectAnnulusCmd>("拉伸矩形环", appService)
                .AddItem<ToolStripMenuItem, MatrixTransformTestCmd>("测试位置变换", appService);
        }

        private static void ReadInFile(OsgObj osgObj, CmdLineArgs commandArgs)
        {
            var osgFileName = commandArgs.Params.FirstOrDefault();
            if (File.Exists(osgFileName))
            {
                try
                {
                    var node = OsgDBEx.ReadNodeFile(osgFileName);
                    if (node.IsVaild())
                    {
                        node.Name = Path.GetFileNameWithoutExtension(osgFileName);
                        osgObj.GroupFolder.AddOrReplaceNode("Models", new[] { node });
                        osgObj.Viewer.Show(node.Bound, ViewMode.ShowAll);
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.GetAllExMessage(), "打开文件错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
    }
}
